package com.cdu.ai_intelligent_teaching_and_training_system.config;

import com.cdu.ai_intelligent_teaching_and_training_system.entity.*;
import com.cdu.ai_intelligent_teaching_and_training_system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class DataInitializer implements CommandLineRunner {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private StudentMapper studentMapper;
    
    @Autowired
    private CourseMapper courseMapper;
    
    @Autowired
    private AssignmentMapper assignmentMapper;
    
    @Autowired
    private ExamMapper examMapper;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    public void run(String... args) throws Exception {        // 初始化测试数据
        initializeTestData();
    }
    
    private void initializeTestData() {   // 创建用户
        // 检查是否已有数据，避免重复初始化
        if (userMapper.selectAll().size() > 0) {
            return;
        }
        
        User teacherUser = new User();
        teacherUser.setUsername("teacher1");
        teacherUser.setEmail("teacher1@example.com");
        teacherUser.setPassword(passwordEncoder.encode("123456"));
        teacherUser.setRole("teacher");
        userMapper.insert(teacherUser);
        
        User studentUser = new User();
        studentUser.setUsername("student1");
        studentUser.setEmail("student1@example.com");
        studentUser.setPassword(passwordEncoder.encode("123456"));
        studentUser.setRole("student");
        userMapper.insert(studentUser);
        
        // 创建教师
        Teacher teacher = new Teacher();
        teacher.setUserId(teacherUser.getUserId());
        teacher.setName("王教授");
        teacher.setEmail("teacher1@example.com");
        teacher.setPassword(passwordEncoder.encode("123456"));
        teacher.setDepartment("计算机科学系");
        teacherMapper.insert(teacher);
        
        // 创建学生
        Student student = new Student();
        student.setUserId(studentUser.getUserId());
        student.setName("张明");
        student.setEmail("student1@example.com");
        student.setPassword(passwordEncoder.encode("123456"));
        student.setMajor("计算机科学与技术");
        student.setEnrollmentYear(2023);
        studentMapper.insert(student);
        
        // 创建课程
        Course course1 = new Course();
        course1.setName("高等数学精讲");
        course1.setDescription("涵盖微积分、极限、导数等基础内容");
        course1.setTeacherId(teacher.getTeacherId());
        courseMapper.insert(course1);
        
        Course course2 = new Course();
        course2.setName("线性代数应用");
        course2.setDescription("学习向量空间、矩阵理论、线性变换等知识");
        course2.setTeacherId(teacher.getTeacherId());
        courseMapper.insert(course2);
        
        // 创建作业
        Assignment assignment1 = new Assignment();
        assignment1.setCourseId(Math.toIntExact(course1.getCourseId()));
        assignment1.setTitle("高等数学 - 单元测试");
        assignment1.setDescription("完成第一章到第三章的习题");
        assignment1.setDueDate(LocalDateTime.now().plusDays(1));
        assignmentMapper.insert(assignment1);
        
        Assignment assignment2 = new Assignment();
        assignment2.setCourseId(Math.toIntExact(course2.getCourseId()));
        assignment2.setTitle("线性代数 - 第5讲");
        assignment2.setDescription("完成矩阵运算相关习题");
        assignment2.setDueDate(LocalDateTime.now().plusDays(3));
        assignmentMapper.insert(assignment2);
        
        // 创建考试
        Exam exam1 = new Exam();
        exam1.setCourseId(course1.getCourseId());
        exam1.setTitle("高等数学期中考试");
        exam1.setDescription("考试范围：第一章到第五章");
        exam1.setExamDate(LocalDateTime.now().plusDays(7));
        examMapper.insert(exam1);
        
        Exam exam2 = new Exam();
        exam2.setCourseId(course2.getCourseId());
        exam2.setTitle("线性代数期末考试");
        exam2.setDescription("考试范围：全书内容");
        exam2.setExamDate(LocalDateTime.now().plusDays(14));
        examMapper.insert(exam2);
    }
} 